//
//  HTTPTool.m
//  INAlienLand
//
//  Created by dllo on 15/6/29.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "HTTPTool.h"
#import "AFNetworking.h"


@implementation HTTPTool




+(void)get:(NSString *)url
      body:(id)body
HTTPResult:(HTTPStyle)style
   success:(void(^)(id result))success
   failure:(void(^)(NSError *error))failure

{
    //1.获取AFN网络请求的管理类
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //2.判断返回的responseobject的数据局类型
    switch (style) {
            //nsdata
        case HTTP:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
            //json
        case JSON:
            
            break;
            //xml类型
        case XML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
            
            
        default:
            break;
    }
    
    
    [manager.requestSerializer setValue:@"Mozilla/5.0 (iPhone; CPU iPhone OS 8_1_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12B466 @BreadTrip/5.1.1/zh-Hans Paros/3.2.13" forHTTPHeaderField:@"User-Agent"];
    [manager.requestSerializer setValue:@"bt_devid=i_549d4f4fa7c35e63e2b086afadb0e7c7ca827f81fc62de7158a415472ba7dcf65ec66da7; Hm_lpvt_39ccc5353de4e1969b414e80e516d5a7=1435894222; Hm_lvt_39ccc5353de4e1969b414e80e516d5a7=1435289251,1435540300,1435824769,1435884631; sessionid=f22ce74edcafac62c96a70f4a461fdce; btuid=49321ee80b5411e5b21ec81f66e3fe07; csrftoken=257b2dd844080beb570242cb0cb6330d" forHTTPHeaderField:@"Cookie"];
    //3.让管理类管理调用请求
   [manager GET:url parameters:body success:^(AFHTTPRequestOperation *operation, id responseObject) {
       //如果成功返回数据
       if (success) {
            success(responseObject  );
       }
      
       
   } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
       //如果失败返回error信息
       if (failure) {
           failure(error);
       }
       
   }];
}


+(void)post:(NSString *)url
       body:(id)body
 HTTPResult:(HTTPStyle)style
RequestBodyStyle:(RequestBodyStyle)bodyStyle
    success:(void(^)(id result))success
    failure:(void(^)(NSError *error))failure
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    switch (bodyStyle) {
        case BodyHTTP:
            manager.requestSerializer = [AFHTTPRequestSerializer serializer];
            break;
        case BodyJSON:
            manager.requestSerializer = [AFJSONRequestSerializer serializer];
            break;
            
        default:
            break;
    }
    
    
    switch (style) {
        case HTTP:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
            
        case JSON:
            
            break;
            
        case XML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
            
        default:
            break;
    }
    [manager POST:url parameters:body success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
        }
        
    }];
    
}
@end
